package com.huatai.manage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.manage.aop.annotation.TrialIdIsExist;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.query.RemoveQuery;
import com.huatai.manage.entity.TbStageMainEntity;
import com.huatai.manage.service.TbStageMainService;
import com.huatai.manage.utils.ComUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 阶段管理 前端控制器
 * </p>
 *
 * @author luoxc
 * @since 2023-03-22
 */
@RestController
@RequestMapping("stageMain")
@Api(value = "某场试验的父阶段", tags = "流程阶段管理")
public class TbStageMainController {

	@Autowired
	private TbStageMainService tbStageMainService;

	@ApiOperationSupport(order = 1, includeParameters = {
		"pageQuery.current",
		"pageQuery.size",
		"pageQuery.data.stageName"
	})
	@PostMapping("pageList")
	@ApiOperation(value = "分页查询", notes = "分页查询")
	public R pageList(@RequestBody PageQuery<TbStageMainEntity> pageQuery) {
		return R.data(this.tbStageMainService.pageList(pageQuery));
	}

	@ApiOperationSupport(order = 2, includeParameters = {
		"pageQuery.current",
		"pageQuery.size",
		"pageQuery.data.trialId"
	})
	@PostMapping("pageByTrialId")
	@ApiOperation(value = "试验id分页阶段列表", notes = "试验id分页阶段列表")
	public R pageByTrialId(@RequestBody PageQuery<TbStageMainEntity> pageQuery) {
		return R.data(this.tbStageMainService.pageByTrialId(pageQuery));
	}

	@PostMapping("listByTrialId")
	@ApiOperation(value = "试验id获取阶段列表", notes = "试验id获取阶段列表")
	public R listByTrialId(@ApiParam(name = "trialId",value = "试验id", required = true) @RequestParam("trialId") String trialId) {
		return R.data(this.tbStageMainService.listByTrialId(trialId));
	}

	@GetMapping("list")
	@ApiOperation(value = "查询所有数据", notes = "查询所有数据")
	public R list() {
		return R.data(this.tbStageMainService.selectAll());
	}

	@PostMapping("getByTrialId")
	@ApiOperation(value = "试验id查询(壹瞰思)", notes = "试验id查询(壹瞰思)")
	public R getByTrialId(@ApiParam(name = "trialId",value = "试验id", required = true) @RequestParam("trialId") String trialId) {
		return R.data(this.tbStageMainService.getByTrialId(trialId));
	}

	@PostMapping("getByStageId")
	@ApiOperation(value = "阶段id查询单条数据", notes = "阶段id查询单条数据")
	public R getOne(@ApiParam(name = "stageId",value = "阶段id", required = true) @RequestParam("stageId") String stageId) {
		return R.data(this.tbStageMainService.getByStageId(stageId));
	}

	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("insert")
	@ApiOperation(value = "新增数据", notes = "新增数据")
	public R insert(@Valid @RequestBody TbStageMainEntity tbStageMainEntity) {
		return R.status(this.tbStageMainService.save(tbStageMainEntity.setStageId(ComUtil.getUUID())));
	}

	@TrialIdIsExist(remark = "校验trialId在数据库中是否存在")
	@PostMapping("update")
	@ApiOperation(value = "修改数据", notes = "修改数据")
	public R update(@Valid @RequestBody TbStageMainEntity tbStageMainEntity) {
		return R.status(this.tbStageMainService.update(tbStageMainEntity));
	}

	@PostMapping("removes")
	@ApiOperation(value = "批量删除", notes = "批量删除")
	public R removes(@RequestBody RemoveQuery remove) {
		return R.status(this.tbStageMainService.removes(remove.getIds()));
	}

}
